//
//  UILabel+Category.m
//  FlowerTown
//
//  Created by lee on 2017/9/21.
//  Copyright © 2017年 TT. All rights reserved.
//

#import "UILabel+Category.h"
#import "UIColor+HZY.h"


@implementation UILabel (Category)

+(UILabel *)labelWithFont:(UIFont*)font textColor:(UIColor *)color
{
    UILabel *temp=[UILabel new];
    temp.font=font;
    temp.textColor=color;
    return temp;
}

-(void)addLineToLabelWithColor:(UIColor *)color
{
    CAShapeLayer *layer=[[CAShapeLayer alloc]initWithLayer:self.layer];
    layer.lineWidth=1;
    layer.strokeColor=color.CGColor;
    layer.fillColor=[UIColor clearColor].CGColor;
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(0, self.frame.size.height/2)];
    [path addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height/2)];
    
    layer.path = path.CGPath;
    
    [self.layer addSublayer:layer];
}
#pragma mark - ************* 计算size ******************
-(CGSize)sizeWithLabelText//单行文字
{
    return [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
}

-(CGSize)sizeWithLabelTextMaxSize:(CGSize)size//多行文字
{
    return [self.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil].size;
}
-(CGSize)sizeWithLabelAttributeTextMaxSize:(CGSize)size//多行富文本
{
    return [self.attributedText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
}



-(void)autoSizeFont
{
    self.numberOfLines=0;
    CGRect frame=self.frame;
    CGSize size=[self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
    frame.size.width=size.width;
    frame.size.height=size.height;
    self.frame=frame;
}
-(void)autoSizeWithFontSize:(CGSize)size
{
    //self.numberOfLines=0;
    CGRect frame=self.frame;
    CGSize size1=[self.text boundingRectWithSize:size options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
    frame.size.width=size1.width;
    frame.size.height=size1.height;
    self.frame=frame;
}
-(void)autoSizeAttributedWithFontSize:(CGSize)size
{
    CGRect frame=self.frame;
    CGSize size2=[self.attributedText boundingRectWithSize:size options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    frame.size.width=size2.width;
    frame.size.height=size2.height;
    self.frame=frame;
}
+(UILabel*)hzLabelWithFrame:(CGRect)frame
                    andText:(NSString*)text
                    andFont:(UIFont*)font
           andTextAlignment:(NSTextAlignment)align
               andTextColor:(NSString*)txtColor andBgColor:(NSString*)bgColor
{
    UILabel* label=[[UILabel alloc]initWithFrame:frame];
    if (text&&![text isKindOfClass:[NSNull class]])
    {
        label.text=text;
    }
    label.font=font;
    label.textAlignment=align;
    label.backgroundColor=[bgColor isEqualToString:@"clear"] ? [UIColor clearColor] : [UIColor colorWithRgbColor:bgColor];
    label.textColor=[UIColor colorWithRgbColor:txtColor];
    return label;
}



//dispatch_source_t _timer;
- (void)startWithTime:(NSInteger)timeLine title:(NSString *)title compelationBlock:(CompelationBlock)compelation
{
    
    //倒计时时间
    __block NSInteger timeOut = timeLine;
    __weak UILabel *ws=self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    if (_timer)
//    {
//        _timer=nil;
//    }
   dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        //倒计时结束，关闭
        if (timeOut <= 0) {
           // [ws setText:[NSString stringWithFormat:@"%@ 00",title]];
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                if (compelation)
                {
                    compelation();
                }
            });
        } else {
            int allTime = (int)timeOut;
            int seconds = timeOut % 60;
            
            NSString *timeStr = [NSString stringWithFormat:@"%0.2d", seconds];
            if (allTime>3600)
            {
                int hour=allTime/3600;
                int mintues=allTime%3600/60;
                timeStr =[NSString stringWithFormat:@"%0.2d:%0.2d:%0.2d",hour,mintues,seconds];
            }
            else if(allTime>60)
            {
                int mintues=allTime/60;
                timeStr =[NSString stringWithFormat:@"%0.2d:%0.2d",mintues,seconds];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [ws setText:[NSString stringWithFormat:@"%@%@",title,timeStr]];
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
}


@end
